<?php
require_once("mysql.inc.php");

if(isset($_GET["theme"])) {
	getAuthors($_GET["theme"]);
}

function getAuthors($theme) {

	$theme = mysql_real_escape_string($theme);
	$result = mysql_query("SELECT * FROM `books` WHERE `theme1`='$theme' OR `theme2`='$theme' ORDER BY `author`") or die(mysql_error());
	$i = 0;
	$authors = array();
	
	while($fetch = mysql_fetch_array($result)) {
		if(in_array($fetch["author"], $authors) == false) {
			$authors[$i] = $fetch["author"];
			$i++;
		}
	}
	
	$num = count($authors);
	
	//create new dom document
	$doc = new DomDocument('1.0');
	$root = $doc->createElement("results");
	$doc->appendChild($root);
	
	//create first element containing amount of results
	$n = $doc->createElement("num");
	$n->appendChild($doc->createTextNode($num));
	$root->appendChild($n);
	
	for($j=1; $j<=$num; $j++) {
		$a = $doc->createElement("author$j");
		$a->appendChild($doc->createTextNode($authors[$j - 1]));
		$root->appendChild($a);
	}
	
	echo $doc->saveXML();
}
?>